﻿// <copyright file="ABTestingCore.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-11-27</date>
// <summary>ABTestingCore class definition</summary>

namespace Microsoft.WebAnalytics.Implementation
{
    using System.ComponentModel.Composition;
    using System.Diagnostics.CodeAnalysis;
    using System.IO.IsolatedStorage;

    /// <summary>
    /// Container for the A/B Testing state name property
    /// </summary>
    public class ABTestingCore
    {
        #region Constants
        /// <summary>
        /// The Isolated storage A/B testing state name key
        /// </summary>
        private const string ABTestingStateNameKey = "Microsoft.WebAnalytics.ABTesting.StateName";
        #endregion

        /// <summary>
        /// The state name
        /// </summary>
        private string stateName;

        #region Properties
        /// <summary>
        /// Gets or sets the A/B Testing state name
        /// </summary>
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Necessary for MEF Exporting."), Export]
        public string ABTestingStateName
        {
            get
            {
                if (!string.IsNullOrEmpty(this.stateName))
                {
                    return this.stateName;
                }

                try
                {
#if SILVERLIGHT
                    if (IsolatedStorageSettings.ApplicationSettings.Contains(ABTestingStateNameKey))
                    {
                        this.stateName = IsolatedStorageSettings.ApplicationSettings[ABTestingStateNameKey] as string;

                        return this.stateName;
                    }
#else
                    this.stateName = Properties.Settings.Default.StateName;

                    return this.stateName;
#endif
                }
                catch (IsolatedStorageException)
                {
                    // Isolated storage is disabled                    
                }

                return null;
            }

            set
            {
                try
                {
                    this.stateName = value;
#if SILVERLIGHT
                    if (value == null)
                    {
                        IsolatedStorageSettings.ApplicationSettings.Remove(ABTestingStateNameKey);
                    }
                    else
                    {
                        IsolatedStorageSettings.ApplicationSettings[ABTestingStateNameKey] = value;
                    }
#else
                    Properties.Settings.Default.StateName = value;
                    
                    Properties.Settings.Default.Save();
#endif
                }
                catch (IsolatedStorageException)
                {
                    // Isolated storage is disabled 
                }
            }
        }

        #endregion

        /// <summary>
        /// Save the state name to isolated storage
        /// </summary>
        /// <param name="newStateName">the new state name</param>
        [Export]
        public void SaveTestingState(string newStateName)
        {
            this.ABTestingStateName = newStateName;
        }
    }
}
